<?php


namespace app\backend\controller;
use app\backend\service\AuthService;
use app\common\RootController;
use app\backend\service\RoleService;

class Role extends Backend
{
    /**
     * 角色列表
     * @param RoleService $roleService
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     * @throws \think\db\exception\DbException
     */
    public function index(RoleService $roleService)
    {
        if ($this->request->isAjax()) {
            return $this->success($roleService->lists($this->request->param()));
        }
        return $this->fetch();
    }

    /**
     * 添加角色
     * @param AuthService $authService
     * @param RoleService $roleService
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(AuthService $authService, RoleService $roleService)
    {
        if ($this->request->isAjax()) {
            $addResult = $roleService->add($this->request->param());
            return $this->success($addResult);
        }
        return $this->fetch('', ['authTree' => $authService->authTree()]);
    }

    /**
     * 编辑角色
     * @param RoleService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(RoleService $service)
    {
        // 请求参数
        $params = $this->request->param();
        // 数据处理
        if ($this->request->isAjax()) {
            $editResult = $service->edit($params);
            return $this->success($editResult);
        }
        return $this->fetch('', ['authTree' => $service->checkAuth($this->request->param('id')),
            'info' => $service->info($params['id'])]);
    }

    /**
     * 删除角色
     * @param RoleService $service
     * @return \think\response\Json
     * @throws \app\common\RootException
     */
    public function del(RoleService $service)
    {
        return $this->success($service->del($this->request->param('roleId')));
    }

    /**
     * 详情
     * @param RoleService $service
     * @return string
     * @throws \app\common\RootException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(RoleService $service)
    {
        $roleId = $this->request->param('id');
        return $this->fetch('', ['authTree' => $service->checkAuth($roleId), 'info' => $service->info($roleId)]);
    }
}